package top.itmeans.yes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.itmeans.yes.dto.ProductAttrInfo;
import top.itmeans.yes.dto.ProductAttributeParam;
import top.itmeans.yes.entity.ProductAttribute;
import top.itmeans.yes.entity.ProductAttributeCategory;
import top.itmeans.yes.mapper.ProductAttributeCategoryMapper;
import top.itmeans.yes.mapper.ProductAttributeMapper;
import top.itmeans.yes.service.ProductAttributeService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 玉货属性参数表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeMapper, ProductAttribute> implements ProductAttributeService {

    @Resource
    private ProductAttributeMapper productAttributeMapper;
    @Resource
    private ProductAttributeCategoryMapper productAttributeCategoryMapper;

    @Override
    public Page<ProductAttribute> getList(Long cid, Integer type, Integer pageSize, Integer pageNum) {
        Page<ProductAttribute> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId,cid).eq(ProductAttribute::getType,type)
                .orderByDesc(ProductAttribute::getId);
        return productAttributeMapper.selectPage(page,queryWrapper);
    }

    @Override
    public int create(ProductAttributeParam productAttributeParam) {
        ProductAttribute productAttribute = new ProductAttribute();
        BeanUtils.copyProperties(productAttributeParam, productAttribute);
        int count = productAttributeMapper.insert(productAttribute);
        //新增商品属性以后需要更新商品属性分类数量
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectById(productAttribute.getProductAttributeCategoryId());
        if(productAttribute.getType()==0){
            productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount()+1);
        }else if(productAttribute.getType()==1){
            productAttributeCategory.setParamCount(productAttributeCategory.getParamCount()+1);
        }
        productAttributeCategoryMapper.updateById(productAttributeCategory);
        return count;
    }

    @Override
    public int update(Long id, ProductAttributeParam productAttributeParam) {
        ProductAttribute productAttribute = new ProductAttribute();
        productAttribute.setId(id);
        BeanUtils.copyProperties(productAttributeParam, productAttribute);
        return productAttributeMapper.updateById(productAttribute);
    }

    @Override
    public ProductAttribute getItem(Long id) {
        return productAttributeMapper.selectById(id);
    }

    @Override
    public int delete(List<Long> ids) {
        //获取分类
        ProductAttribute productAttribute = productAttributeMapper.selectById(ids.get(0));
        Integer type = productAttribute.getType();
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectById(productAttribute.getProductAttributeCategoryId());
        int count = productAttributeMapper.deleteBatchIds(ids);
        //删除完成后修改数量
        if(type==0){
            if(productAttributeCategory.getAttributeCount()>=count){
                productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount()-count);
            }else{
                productAttributeCategory.setAttributeCount(0);
            }
        }else if(type==1){
            if(productAttributeCategory.getParamCount()>=count){
                productAttributeCategory.setParamCount(productAttributeCategory.getParamCount()-count);
            }else{
                productAttributeCategory.setParamCount(0);
            }
        }
        productAttributeCategoryMapper.updateById(productAttributeCategory);
        return count;
    }

    @Override
    public List<ProductAttrInfo> getProductAttrInfo(Long productCategoryId) {
        return productAttributeMapper.getProductAttrInfo(productCategoryId);
    }
}
